package com.ukao.cashregister.ui.vipManager;


import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.OftenPlaceAdapter;
import com.ukao.cashregister.base.MvpFragment;
import com.ukao.cashregister.bean.AddressList;
import com.ukao.cashregister.bean.UserInfoBean;
import com.ukao.cashregister.dialog.InquiryDialogFragment;
import com.ukao.cashregister.eventbus.ChooseTimeEvent;
import com.ukao.cashregister.listener.MyOnItemClickListener;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.listener.OnItemChildClickListener;
import com.ukao.cashregister.pesenter.OftenPlacePesenter;
import com.ukao.cashregister.ui.createOrders.UserInfoRightAddAddressFragment;
import com.ukao.cashregister.utils.StackManager;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.view.OftenPlaceView;

import org.greenrobot.eventbus.Subscribe;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 常用地址
 */
public class OftenPlaceFragment extends MvpFragment<OftenPlacePesenter> implements OftenPlaceView{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.address_recycler_view)
    RecyclerView addressRecyclerView;
    @BindView(R.id.add_address_layout)
    LinearLayout addAddressLayout;
    Unbinder unbinder;


    private String mParam2;
    private OftenPlaceAdapter mOftenPlaceAdapter;
    private List<AddressList> mData = new ArrayList();
    private boolean isloadfinish = false;
    private String useId;
    private UserInfoBean.UserBean useIdParam;
    public AddressList item;

    public static OftenPlaceFragment newInstance(Serializable useIdParam, String param2) {
        OftenPlaceFragment fragment = new OftenPlaceFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, useIdParam);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            useIdParam = (UserInfoBean.UserBean) getArguments().getSerializable(ARG_PARAM1);
            useId = useIdParam.getInfo().getId();
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected OftenPlacePesenter createPresenter() {
        return new OftenPlacePesenter(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_often_place, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }


    @Override
    protected void initView() {

        initLinearRecyclerView(addressRecyclerView);
        mOftenPlaceAdapter = new OftenPlaceAdapter(activity, mData);
        addressRecyclerView.setAdapter(mOftenPlaceAdapter);

    }

    @Override
    protected void initListener() {
        super.initListener();
        mOftenPlaceAdapter.setOnItemClickListener(mOnItemClickListener);
        mOftenPlaceAdapter.setOnItemChildClickListener(mOnItemChildClickListener);
    }



    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!isloadfinish)
            mvpPresenter.getAddressRequest(_mActivity, useId);
    }





    private OnItemChildClickListener mOnItemChildClickListener = new OnItemChildClickListener() {

        @Override
        public void onChildItemClick(int id, int pos) {
             item= mOftenPlaceAdapter.getItem(pos);
             if (item==null)return;
                        switch (id){
                            case  R.id.edit_btn:
                                if (getParentFragment() instanceof VipDetailsTabFragment) {
                                    ((VipDetailsTabFragment) getParentFragment()).start(UserInfoRightAddAddressFragment.newInstance(item));
                                }

                                break;
                            case  R.id.delete_btn:

                                InquiryDialogFragment mInquiryDialog=new InquiryDialogFragment();
                                mInquiryDialog.setOnSureClickListener(mOnDialogDelete);
                                mInquiryDialog.show(getChildFragmentManager(),InquiryDialogFragment.class.getName());


                                break;
                        }
        }
    };

    private OnDialogSureClickListener mOnDialogDelete=new OnDialogSureClickListener() {
        @Override
        public void onSureItemClick(Object message) {
            mvpPresenter.addressDelete(item);
        }
    };


    private OnItemClickListener mOnItemClickListener = new MyOnItemClickListener() {


        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            super.onItemClick(viewGroup, view, o, i);

            AddressList       defaultitem= (AddressList) o;
            if (defaultitem.getDef()!=1) {
                mvpPresenter.addressDefault(defaultitem,i);


            }


        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.add_address_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.add_address_layout:
                UserInfoBean.UserBean.InfoBeanX info = useIdParam.getInfo();
               UserInfoRightAddAddressFragment addAddress = UserInfoRightAddAddressFragment.newInstance(info.getPhone(), info.getName(), info.getId(), info.getSex(),true);
                StackManager.getInstance().popFragmant(_mActivity,this, addAddress, R.id.user_info_right_container);
                break;
        }
    }


    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Subscribe
    public void onEvent(ChooseTimeEvent event) {

        switch (event.getMessage()) {

            case sendaddress:  /*添加地址成功*/
                isloadfinish=false;
                mvpPresenter.getAddressRequest(_mActivity, useId);
                break;
        }

    }

    @Override
    public void loadAddressSuccess(List<AddressList> AddressList) {
        isloadfinish = true;
        mOftenPlaceAdapter.setDatas(AddressList);
        float weight = 0f;
        if (mOftenPlaceAdapter.getDatas().size() > 4) {
            weight = 1.0f;
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, weight);
        addressRecyclerView.setLayoutParams(lp);
    }

    @Override
    public void loadFail(String message) {
        T.show(message);
    }

    @Override
    public void deleteSuccess(AddressList item, String msg) {
        mOftenPlaceAdapter.getDatas().remove(item);
        mOftenPlaceAdapter.notifyDataSetChanged();
        T.show(msg);


    }

    @Override
    public void addressDefault(String msg, AddressList defaultitem, int pos) {
        defaultitem.setDef(1);/*设为默认*/
        List<AddressList> mData = mOftenPlaceAdapter.getDatas();
        for (int j = 0; j < mData.size(); j++) {
            if (pos != j) {
                mData.get(j).setDef(0);

            }
        }

        mOftenPlaceAdapter.notifyDataSetChanged();
    }


}
